﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using Jy123.Web.AppCode;
using System.Collections.Generic;

namespace Jy123.Web.Gift
{
    public partial class ReceiveGift : System.Web.UI.Page
    {
        private int userID ;
        protected void Page_Load(object sender, EventArgs e)
        {
            List<int?> count;
            int currentpage = 1;//当前页
            int pagesize = 4;//页记录大小

            //把被查询页赋值给当前页
            if (!string.IsNullOrEmpty(Request.QueryString["page"]))
            {
                currentpage = Convert.ToInt32(Request.QueryString["page"]);
            }

            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);

            string strUserGuestID = Request.QueryString["userID"];
            if (!string.IsNullOrEmpty(strUserGuestID))
            {
                userID = Convert.ToInt32(strUserGuestID);
            }

            #region 公共部分
            StringBuilder strHtml = new StringBuilder(Cache["user_giftreceive"].ToString());//取得主HTML页面
            strHtml = strHtml.Replace("$Header$", Cache["user_header2"].ToString());
            strHtml = strHtml.Replace("$Footer$", Cache["user_footer"].ToString());
            strHtml = strHtml.Replace("$LeftUserCmd$", Cache["user_navigation_down"].ToString());
            strHtml = strHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());
            //统计站内信数量
            Jy123.BLL.User.Letter bllLetter = new Jy123.BLL.User.Letter();
            int newLetterCount = bllLetter.CountNewLetter(userID);
            strHtml = strHtml.Replace("$NewMsg$", newLetterCount.ToString());
            #endregion

            string strReceiveList = Cache["user_giftreceive_li"].ToString();
            StringBuilder sbReceiveList = new StringBuilder();
            DataTable dtReceiveList = new Jy123.BLL.User.Gift().GiftRecord(currentpage, pagesize, "FReciever='" + userID + "'",out count).Tables[0];
            for (int i = 0; i < dtReceiveList.Rows.Count; i++)
            {
                string str = strReceiveList;
                if (userID != int.Parse(strUserIdFromCookie))
                {
                    str = str.Replace("删了它？",string.Empty);
                }
                
                str = str.Replace("$GiftFilePath$", dtReceiveList.Rows[i]["Picture"].ToString())
                    .Replace("$Description$", dtReceiveList.Rows[i]["Decription"].ToString())
                    .Replace("$CreatTime$", dtReceiveList.Rows[i]["CreatTime"].ToString())
                    .Replace("$FUser$", dtReceiveList.Rows[i]["FUser"].ToString())
                    .Replace("$FUserName$", dtReceiveList.Rows[i]["FUserName"].ToString())
                    //.Replace("$Privacy$", dtReceiveList.Rows[i]["Privacy"].ToString())
                    .Replace("$GMessage$", dtReceiveList.Rows[i]["GMessage"].ToString())
                    .Replace("$RecordID$", dtReceiveList.Rows[i]["ID"].ToString());
                string privacy = dtReceiveList.Rows[i]["Privacy"].ToString();
                switch (privacy)
                {
                    case "0": privacy = "公开"; break;
                    case "1": privacy = "网络"; break;
                    case "2": privacy = "好友"; break;
                    case "3": privacy = "自己"; break;
                    default: privacy = "公开"; break;
                }
                str = str.Replace("$Privacy$", privacy);
                sbReceiveList.Append(str);
            }
            string pageHtml = @"
                            <li class=""page"">
                                <span>
                                       {0}
                                </span>
                                <b>第{1}-{2}个/共{3}个</b>
                            </li>";
            string strHref = "ReceiveGift.aspx?page=$num$";
            MoonPage mp = new MoonPage(false, true);
            mp.Count = Convert.ToInt32(count[1]);
            sbReceiveList.AppendFormat(pageHtml, mp.GetStrPage(currentpage, "no", "sel", strHref), 1 + (currentpage - 1) * pagesize, ((currentpage * pagesize) > count[0]) ? count[0] : currentpage * pagesize, count[0]);
            
            strHtml = strHtml.Replace("$GiftReceive_li$", sbReceiveList.ToString());
            Response.Write(strHtml.ToString());
        }
    }
}
